package com.etc.order.provider.mapper;

import com.etc.order.common.domain.ShoppingCart;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ShoppingCartMapper {
    @Select("SELECT * FROM shopping_cart WHERE uid = #{uid}")
    @Results(id = "shoppingCartMap", value = {
            @Result(column = "cart_id", property = "cartId"),
            @Result(column = "uid", property = "uid"),
            @Result(column = "product_id", property = "productId"),
            @Result(column = "product_quantity", property = "productQuantity")
    })
    List<ShoppingCart> findByUid(@Param("uid") int uid);

    @Delete("DELETE FROM shopping_cart WHERE uid = #{uid}")
    int deleteByUid(@Param("uid") int uid);

    @Delete("DELETE FROM shopping_cart WHERE uid = #{uid} AND product_id = #{productId}")
    int deleteByUidAndProductId(@Param("uid") int uid, @Param("productId") String productId);

    @Insert("INSERT INTO shopping_cart VALUES(default, #{cart.uid}, #{cart.productId}, #{cart.productQuantity})")
    int insertShoppingCart(@Param("cart") ShoppingCart cart);

    @Update("UPDATE shopping_cart SET product_quantity = #{cart.productQuantity} WHERE uid = #{cart.uid} AND product_id = #{cart.productId}")
    int updateShoppingCart(@Param("cart") ShoppingCart cart);
}
